package com.sduept.nwld.dataserver.rest.fault;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sduept.bigdata.fault.entity.FaultRecord;
import com.sduept.bigdata.fault.manager.FaultManager;
import com.sduept.bigdata.fault.manager.FaultRecordQueryManager;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author laiy
 */
@RestController
@RequestMapping("/lineprotect/")
public class LineProtectActServices {

	@Autowired
	private SuperDAO dao;
	@Autowired
	private CimManager cim;
	@Autowired
	private FaultManager bx;
	@Autowired
	private FaultRecordQueryManager fqm;


	/**
	 * 获取线路故障时间列表
	 * */

	@GetMapping("/getLineFaultData/{params}")
	public String getLineFaultData(@PathVariable("params") String params) {
		//获取当前年份
		Calendar calendar = Calendar.getInstance();   
		int year = calendar.get(Calendar.YEAR);  //当前年
		
		JSONObject jsonObject = JSON.parseObject(params);
		String beginTime = (String) jsonObject.get("beginTime");
		String endTime = (String) jsonObject.get("endTime");
		String lineId = (String) jsonObject.get("lineId");
		/*String[] equipmentIds = new String[1];
		equipmentIds[0]=lineId;*/
		//List<FaultRecord> faultList=new ArrayList<>();
		//faultList=bx.find500KvFault(beginTime.equals("")?year+"-01-01 00:00:00":beginTime+" 00:00:00",endTime.equals("")?year+"-12-31 00:00:00":endTime+" 00:00:00","线路",equipmentIds);
		Date start = DateUtil.getDateByString(beginTime.equals("")?year+"-01-01 00:00:00":beginTime+" 00:00:00", DateUtil.pattern_ymdhms);
		Date end = DateUtil.getDateByString(endTime.equals("")?year+"-12-31 00:00:00":endTime+" 00:00:00", DateUtil.pattern_ymdhms);
		List<FaultRecord> faultList=fqm.findFaultByTimeDeviceId(start, end, lineId);//bx.find500KvFault(beginTime.equals("")?year+"-01-01 00:00:00":beginTime+" 00:00:00",endTime.equals("")?year+"-12-31 00:00:00":endTime+" 00:00:00","线路",equipmentIds);
		List<String> timeList=new ArrayList<>();
		for(int i=0;i<faultList.size();i++){
			timeList.add(faultList.get(i).getFaultTime().toString());
		}
		Collections.sort(timeList,new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				long t1 = DateUtil.getDateByString(o1, DateUtil.pattern_ymdhms).getTime();
				long t2 = DateUtil.getDateByString(o2, DateUtil.pattern_ymdhms).getTime();
				if(t1>t2){
					return -1;
				}else if(t1==t2){
					return 0;
				}else{
					return 1;
				}
			}
		  });
		return JSONArray.toJSONString(timeList);
	}


	
}
